<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div{
            width: 100px;
            height: 100px;
            background-color: hotpink;
            position: absolute;
            left:100px;
            top:100px;
        }
    </style>
</head>
<body>
    
  <div></div>

</body>
</html>
<script src='../../util/Util.js'></script>
<script>
  
  let divObj = document.getElementsByTagName('div')[0];

  document.onkeydown=(e)=>{

    //获得div元素的left 和 top
    let div_left =parseInt(getStyle(divObj,'left'));
    let div_top =parseInt(getStyle(divObj,'top'));

    //获得事件对象
    let eventObj = window.event||e;

     //判断按下了那个键
     //左
     if(eventObj.keyCode==37){
        div_left-=100; 
     }else //上
      if(eventObj.keyCode==38){
        div_top-=100;
     }else //右
      if(eventObj.keyCode==39){
        div_left+=100;
      }else  //下 
      if(eventObj.keyCode==40){
        div_top+=100;
      }

       divObj.style.left=div_left+'px';
       divObj.style.top=div_top+'px';
  }


</script>